<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>mongodb 慢查询</title>
  
    <meta name="author" content="秀才">

    <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Le styles -->
    <link href="/assets/twitter/stylesheets/bootstrap.min.css" type="text/css" rel="stylesheet" media="all">
<link href="/assets/twitter/stylesheets/style.css" type="text/css" rel="stylesheet" media="all">
 
    <link href="/assets/twitter/widgets/google_prettify/stylesheets/twitter-bootstrap.css" type="text/css" rel="stylesheet" media="all">

    <!-- Le fav and touch icons -->
  <!-- Update these with your own images
    <link rel="shortcut icon" href="images/favicon.ico">
    <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
    <link rel="apple-touch-icon" sizes="72x72" href="images/apple-touch-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="114x114" href="images/apple-touch-icon-114x114.png">
  -->
  </head>

  <body>

    <div class="navbar">
      <div class="navbar-inner">
        <div class="container">
          <a class="brand" href="/">秀才</a>
          <ul class="nav">
            
              


  <li><a href="/tags">标签</a></li>


            
              


  <li><a href="/categories">分类</a></li>


            
              


  <li><a href="/about">关于我</a></li>


            
          </ul>
        </div>
      </div>
    </div>

    <div class="container">
      <div class="content">
        <div class="page-header">
  <h1>mongodb 慢查询 </h1>
</div>

<div class="row">
  <div class="span12">
    <h3>慢查询设置</h3>

<p>传送门<a href="https://docs.mongodb.com">官方wiki</a></p>

<p><strong>获取数据库当前的查询记录级别:</strong></p>

<pre><code class="language-js">db.getProfilingLevel()：     // 0 - 不开启 | 1 - 记录慢命令 | 2 - 记录所有命令
</code></pre>

<p><strong>获取数据库当前的查询记录状态:</strong></p>

<pre><code class="language-js">db.getProfilingStatus();    // was - 状态级别1/2/3 | slowms - 慢查询时间限制
</code></pre>

<p><strong>设置数据库当前的查询记录级别：</strong></p>

<pre><code class="language-js">db.setProfilingLevel(0|1,slowms|2)： //  设置为1时，需要同时设置慢查询时间限制-slowms  
</code></pre>

<p><strong>慢查询日志过滤：(简单按照时间过滤)</strong></p>

<p>正常的查询语句即可</p>

<pre><code class="language-js">db.system.profile.count({millis:{$gt:500}});
</code></pre>

<p><strong>删除查询记录：(需要设置查询记录级别为0后，才可以执行成功)</strong></p>

<pre><code class="language-js">db.system.profile.drop();
</code></pre>

<h3>慢查询内容分析</h3>

<p>例如，运行下面js：</p>

<pre><code class="language-js">db.system.profile.find().sort({ ts : -1 })
</code></pre>

<p>结果：</p>

<pre><code class="language-json">/* 1 */
{
    &quot;op&quot; : &quot;getmore&quot;,	
    &quot;ns&quot; : &quot;pigcome.enemiesV1&quot;,	// 操作表
    &quot;query&quot; : {	
        &quot;getMore&quot; : NumberLong(372388297001),
        &quot;collection&quot; : &quot;enemiesV1&quot;
    },
    &quot;cursorid&quot; : 372388297001.0, // 游标(读取文件的游标)
    &quot;cursorExhausted&quot; : true, // 
    &quot;keyUpdates&quot; : 0, 
    &quot;writeConflicts&quot; : 0,
    &quot;numYield&quot; : 34, // 可以想象一下cpu的原理，挂起34次才完成操作
    &quot;locks&quot; : {
        &quot;Global&quot; : {
            &quot;acquireCount&quot; : {
                &quot;r&quot; : NumberLong(70)
            }
        },
        &quot;Database&quot; : {
            &quot;acquireCount&quot; : {
                &quot;r&quot; : NumberLong(35)
            }
        },
        &quot;Collection&quot; : {
            &quot;acquireCount&quot; : {
                &quot;r&quot; : NumberLong(35)
            }
        }
    },
    &quot;nreturned&quot; : 4407,
    &quot;responseLength&quot; : 516828,
    &quot;protocol&quot; : &quot;op_query&quot;,
    &quot;millis&quot; : 102,
    &quot;execStats&quot; : {},
    &quot;ts&quot; : ISODate(&quot;2018-01-19T17:42:52.183+08:00&quot;),
    &quot;client&quot; : &quot;10.25.141.100&quot;,
    &quot;allUsers&quot; : [ 
        {
            &quot;user&quot; : &quot;root&quot;,
            &quot;db&quot; : &quot;admin&quot;
        }
    ],
    &quot;user&quot; : &quot;root@admin&quot;
}
</code></pre>

<ul>
<li>op :</li>
</ul>

    <hr>
    <div class="pagination">
      <ul>
        <ul>
          
            <li class="prev"><a href="/go/go-%E5%B9%B6%E5%8F%91%E5%A4%84%E7%90%86%E8%84%9A%E6%9C%AC/" title="go 并发处理脚本">&larr; Previous</a></li>
          
          

            <li><a href="/archive">Archive</a></li>

          
            <li class="next"><a href="/go/%E5%AD%97%E5%85%B8%E6%A0%91---GO%E7%9A%84%E5%AE%9E%E7%8E%B0/" title="字典树 - GO的实现">Next &rarr;</a></li>
          
          
        </ul>
      </ul>
    </div>
    <hr>
    
  </div>
  
  <!-- <div class="span4">
    <h4>Published</h4>
    <div class="date"><span>2018-01-08</span></div>
    <br>
    <h4>Categories</h4>
    <ul class="tag_box">
    
      <li>
  <a href="/categories/#mongodb-ref">mongodb <span>2</span></a>
</li>
    
    </ul>
    <br>
    <h4>Tags</h4>
    <ul class="tag_box">
    
      <li>
  <a href="/tags/#mongodb-ref">mongodb <span>2</span></a>
</li>
    
    </ul>
  </div> -->
</div>

      </div>

      <footer>
        <p>&copy; 秀才 2013 
          with help from <a href="http://github.com/wendal/gor" target="_blank" title="Gor -- Fast Blog">Gor</a>
          and <a href="http://twitter.github.com/bootstrap/" target="_blank">Twitter Bootstrap</a>
		  and Idea from <a href="http://ruhoh.com" target="_blank" title="The Definitive Technical Blogging Framework">ruhoh</a>
        </p>
      </footer>

    </div> <!-- /container -->
    <!-- <script src="//cdnjscn.b0.upaiyun.com/libs/prettify/r298/prettify.min.js"></script> -->
    <script src="http://cdn.staticfile.org/prettify/r298/prettify.min.js"></script>
    <script>
      var pres = document.getElementsByTagName("pre");
      for (var i=0; i < pres.length; ++i) {
        pres[i].className = "prettyprint linenums";
      }
      prettyPrint();
    </script>
    
    
  </body>
</html>
